{% extends 'layout/manage.html' %}
{% load static %}

{% block title %} {% endblock %}
{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/editor.md-master/css/editormd.min.css' %}">

    <style>
        .panel-default {
            margin-top: 10px;
        }

        .panel-default .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }

        .panel-body {
            padding: 0;
        }

        .title-list {
            border-right: 1px solid #dddddd;
            min-height: 600px;
        }

        .title-list ul {
            padding-left: 15px;
        }

        .title-list ul a {
            display: block;
            padding: 5px 0;
        }

        .content {
            border-left: 1px solid #dddddd;
            min-height: 700px;
            margin-left: -1px;
        }

        .editormd-fullscreen{
            z-index: 1001;
        }
    </style>

{% endblock %}


{% block content %}
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <div><i class="fa fa-book" aria-hidden="true"></i>&nbsp; wiki文档</div>
                <div class="function">
                    <a type="button" class="btn btn-success btn-xs"
                       href="{% url 'web:wiki_add' project_id=request.tracer.project.id %}">
                        <i class="fa fa-plus-circle" aria-hidden="true"></i> 新建
                    </a>
                </div>
            </div>
            <div class="panel-body">
                <div class="col-sm-3 title-list">
                    <ul id="catalog"></ul>

                </div>

                <div class="col-sm-9 content">
                    <form id="wiki_add_form" method="post">
                        {% csrf_token %}
                        {% for field in form %}
                            {% if field.name == 'content' %}
                                <div class="form-group">
                                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                                    <div id="editor"> {{ field }} </div>
                                    <span class="error-msg">{{ field.errors.0 }}</span>
                                </div>
                            {% else %}
                                <div class="form-group">
                                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                                    {{ field }}
                                    <span class="error-msg">{{ field.errors.0 }}</span>
                                </div>
                            {% endif %}
                        {% endfor %}
                        <button type="submit" class="btn btn-primary"> 提 交</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
    </div>

{% endblock %}

{% block js %}
    <script src="{% static 'plugin/editor.md-master/editormd.min.js' %}"></script>
    <script>
        const WIKI_DETAIL_URL = "{% url 'web:wiki_detail' project_id=request.tracer.project.id %}"
        const WIKI_UPLOAD_URL = "{% url 'web:wiki_upload' project_id=request.tracer.project.id %}"

        $(function () {
            // 初始化，显示wiki文章目录
            initCatalog();
            // 初始化，将文本域转换为markdown插件
            initEditor();
        });
        function initEditor() {
            editormd('editor', {
                placeholder: "请输入内容",
                height: 500,
                path: "{% static 'plugin/editor.md-master/lib/' %}",
                imageUpload: true,
                imageFormats: ['jpg', 'jpeg', 'png', 'gif'],
                imageUploadURL: WIKI_UPLOAD_URL
            });
        }

        function initCatalog() {
            $.ajax({
                url: "{% url 'web:wiki_catalog' project_id=request.tracer.project.id %}",
                type: "GET",
                dataType: "JSON",
                success: function (res) {
                    if (res.status) {
                        $.each(res.data, function (index, item) {
                            const href = WIKI_DETAIL_URL + "?wiki_id=" + item.id;
                            const li = $("<li>").attr("id", "id_" + item.id).append($("<a>").text(item.title).attr('href', href)).append($("<ul>"));
                            if (!item.parent_id) {
                                // 生成标签 <li><a>目录</a></li>
                                // 添加到catalog中
                                $('#catalog').append(li);
                            } else {
                                $("#id_" + item.parent_id).children("ul").append(li);
                            }
                        })
                    } else {
                        alert("项目初始化失败！！！");
                    }
                }
            });

        }
    </script>
{% endblock %}